# bagels 游戏
import random

"""
 随机生成一个数字（以字符串的形式出现）
"""


def getSecretNum(num):
    numDigits = list(range(10))
    random.shuffle(numDigits)
    secretNum = ""
    for i in range(num):
        secretNum += str(numDigits[i])
    return secretNum


def showGameRule():
    print("""
I am thinking of a 3-digit number. Try to guess what it is.
The clues I give are...
When I say:    That means:
  Bagels       None of the digits is correct.
  Pico         One digit is correct but in the wrong position.
  Fermi        One digit is correct and in the right position.
I have thought up a number. You have 10 guesses to get it.
    """)


def checkGuess(guess, secret):
    if guess == secret:
        return "Great~ got it~"
    check = []
    for i in range(len(guess)):
        if guess[i] == secret[i]:
            check.append("Fermi")
        elif guess[i] in secret:
            check.append("Pico")
    if len(check) == 0:
        check.append("bagels")

    check.sort()

    return " ".join(check)


def isNumber(num):
    if num == "":
        return False

    for i in num:
        if i not in "0,1,2,3,4,5,6,7,8,9".split(","):
            return False

    return True


def isPlayAgain():
    print("Do u want to play again?")
    answer = input().lower()
    return answer.startswith('y')


_max_times = 10
_num_long = 3

if __name__ == '__main__':
    while True:
        showGameRule()
        times = 0
        print('I have thought up a number. Its long is %s You have %s guesses to get it.' %
              (_num_long, _max_times))
        secret = getSecretNum(_num_long)
        while times <= _max_times:
            times = times + 1
            guess = ' '
            while not isNumber(guess) or len(guess) != _num_long:
                print("Enter your guess:")
                guess = input()
            print(checkGuess(guess, secret))

            # 释放while资源
            if guess == secret:
                print("right~")
                break
            if times > _max_times:
                print('You ran out of guesses. The answer was %s.' % (secret))

        if not isPlayAgain():
            break
